//
//  QSVisitHeaderView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/29.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSVisitHeaderView.h"
#import "QSVisitNumberCell.h"
#import "QSVisitInterFace.h"
#import "EBDropdownListView.h"
#import "QSVisitInfoGroupListModel.h"
@interface QSVisitHeaderView()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UICollectionView *collectionView;
//下拉
@property (nonatomic, strong)EBDropdownListView *dropdownListView;
//下拉数据数组
@property (nonatomic, strong) NSMutableArray *dataArr;
//包含num的数组
@property (nonatomic, strong) QSVisitHeaderInfoModel *model;
@end

static NSString * const visitNumberCell = @"VisitNumberCell";
@implementation QSVisitHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
        [self configSubView];
        [self getApiHeaderVisitInfo];
    }
    return self;
}

- (void)configSubView{
    [self addSubview:self.nameLabel];
    
//    [self addSubview:self.selectPeopleButton];
     
    [self addSubview:self.collectionView];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kRealValue(16));
        make.top.equalTo(self).offset(kRealValue(15));
    }];
//    [self.selectPeopleButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self).offset(- kRealValue(16));
//        make.centerY.equalTo(self.nameLabel);
//        make.width.offset(kRealValue((kScreenWidth - 30) / 3));
//    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(20);
        make.left.right.equalTo(self);
        make.height.offset(kRealValue(107));
    }];
    NSString *name;
    if (kCurrentUser.enterpriseLevel.integerValue == 1) {//服务人员 //epType为1 驻企服务员 epType为2 乡镇联企干部  epType为3 区联企领导
        if (kCurrentUser.epType.integerValue == 1) {
            name = @"驻企服务员";
        }
        if (kCurrentUser.epType.integerValue == 2) {
            name = @"乡镇联企干部";
        }
        if (kCurrentUser.epType.integerValue == 3) {
            name = @"区联企领导";
        }
    }else{
        if (kCurrentUser.enterpriseLevel.integerValue == 2) {
            name = @"组长";
        }else{
            name = @"领导";
        }
    }
    NSInteger lenth = kCurrentUser.name.length;
    _nameLabel.text = [NSString stringWithFormat:@"尊敬的 %@ 您好！", kCurrentUser.name];
    [_nameLabel setAttributedText:[UILabel setLabelTextColorWithString:_nameLabel.text Range:NSMakeRange(4, lenth) andColor:[UIColor colorWithHexString:@"#128BEC"]]];
}
//配置下拉列表项
- (void)configDropDownView{
    _dropdownListView = [[EBDropdownListView alloc] initWithFrame:CGRectMake(kScreenWidth - kRealValue(190), 5, kRealValue(160), kRealValue(34))];
    if (kCurrentUser.enterpriseLevel.integerValue == 2) {
        _dropdownListView.defaultText = @"全部人员";
    }else{
        _dropdownListView.defaultText = @"全部小组";
    }
    _dropdownListView.dataSource = self.dataArr;
    WeakSelf(weakSelf);
    [_dropdownListView setDropdownListViewSelectedBlock:^(EBDropdownListView *dropdownListView) {
        if (weakSelf.visitHeaderViewDidFinishLoadOpenList) {
            NSLog(@"%@", dropdownListView.groupModel.epId);
            weakSelf.visitHeaderViewDidFinishLoadOpenList(dropdownListView.groupModel);
        }
        [weakSelf.collectionView reloadData];
    }];
//    [_dropdownListView setViewBorder:0.5 borderColor:[UIColor grayColor] cornerRadius:2];

    [self addSubview:self.dropdownListView];
}

#pragma mark - Lazy loading
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel labelWithName:@"" font:[UIFont systemFontOfSize:15 weight:1] textColor:[UIColor QSColorBlack434343] textAlignment:NSTextAlignmentLeft];
    }
    return _nameLabel;
}

- (UIButton *)selectPeopleButton{
    if (!_selectPeopleButton) {
        _selectPeopleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_selectPeopleButton setTitle:@"全组人员" forState:UIControlStateNormal];
        [_selectPeopleButton setImage:[UIImage imageNamed:@"ic_selectpeople"] forState:UIControlStateNormal];
        [_selectPeopleButton setButtonRightImageWithButton:_selectPeopleButton];
        [_selectPeopleButton setTitleColor:[UIColor QSColorBlack434343] forState:UIControlStateNormal];
        _selectPeopleButton.titleLabel.font = [UIFont QSFontOfSize14];
    }
    return _selectPeopleButton;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        if (kCurrentUser.enterpriseLevel.integerValue == 1) {
            //服务员没有切换组员/组长项
    
        }else if (kCurrentUser.enterpriseLevel.integerValue == 2){
            NSDictionary *dic = @{@"adminName" : @"全部组员",
                                  @"epId" : @""
            };
            QSVisitInfoGroupListModel *model = [QSVisitInfoGroupListModel mj_objectWithKeyValues:dic];
            [self.dataArr addObject:model];
        }else{
            NSDictionary *dic = @{@"egName" : @"全部小组",
                                  @"egId" : @"",
            };
            QSVisitInfoGroupListModel *model = [QSVisitInfoGroupListModel mj_objectWithKeyValues:dic];
                       [self.dataArr addObject:model];
        }
    }
    return _dataArr;
}



- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        NSInteger Num;
        if (kCurrentUser.enterpriseLevel.integerValue == 1) {
            Num = 3;
        }else{
            Num = 4;
        }
        layout.itemSize = CGSizeMake((kScreenWidth - 30) / Num, kRealValue(107));
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        _collectionView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
        _collectionView.layer.cornerRadius = 6;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[QSVisitNumberCell class]forCellWithReuseIdentifier:visitNumberCell];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    
    return _collectionView;
}

#pragma mark - CollectionView Delegate & DataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (kCurrentUser.enterpriseLevel.intValue == 1) { //组员
        return 3;
    }else{
        return 4;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QSVisitNumberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:visitNumberCell forIndexPath:indexPath];
    if (cell) {
        if (kCurrentUser.enterpriseLevel.integerValue == 1) {
            switch (indexPath.item) {
                    case 0:
                    {
                        cell.numberLabel.text = [NSString stringWithFormat:@"%@", _model.companyNums];
                        cell.textLabel.text = @"我的服务企业";
                        break;
                    }
                    case 1:
                    {
                        cell.numberLabel.text = [NSString stringWithFormat:@"%@", _model.todayVisits];
                        cell.textLabel.text = @"今日走访";
                        break;
                    }
                    case 2:
                    {
                        cell.numberLabel.text = [NSString stringWithFormat:@"%@", _model.currentMonthVisits];
                        cell.textLabel.text = @"本月走访";
                        break;
                    }
                    default:
                        break;
            }
            
        }else{
            switch (indexPath.item) {
                case 0:
                {
                    if (kCurrentUser.enterpriseLevel.integerValue == 2) {
                        cell.textLabel.text = @"我的组员";
                    }else{
                        cell.textLabel.text = @"服务小组";
                    }
                    cell.numberLabel.text = _model.countNums;
                    break;
                }
                case 1:
                {
                    if (kCurrentUser.enterpriseLevel.integerValue == 2) {
                        cell.textLabel.text = @"总服务业";
                    }else{
                        cell.textLabel.text = @"企业服务人员";
                    }
                    cell.numberLabel.text = _model.companyNums;
                    break;
                }
                case 2:
                {
                    cell.textLabel.text = @"今天总走访";
                    cell.numberLabel.text = _model.todayVisits;
                    break;
                }
                case 3:
                {
                    cell.textLabel.text = @"本月总走访";
                    cell.numberLabel.text = _model.currentMonthVisits;
                    break;
                }
                default:
                    break;
        }
        }
    }
    
    return cell;
}

#pragma mark - Request
- (void)getApiHeaderVisitInfo{
    NSString *key;
    if (kCurrentUser.enterpriseLevel.integerValue == 1) {
        key = @"companyList";
    }else if (kCurrentUser.enterpriseLevel.integerValue == 2){
        key = @"personList";
    }else{
        key = @"groupList";
    }
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    [QSVisitInterFace getApiVisitDetail:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSVisitHeaderInfoModel * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            [QSAppKeyWindow hideHud];
            if (kCurrentUser.enterpriseLevel.integerValue == 1) {
                //服务员没有切换组员/组长项
                [self.dataArr addObjectsFromArray: result.companyList];
            }else if (kCurrentUser.enterpriseLevel.integerValue == 2){
                [self.dataArr addObjectsFromArray: result.personList];
                if (self.dataArr.count) {
                    [self configDropDownView];
                }
                self.roleName = result.egName;
//                [self updateUi];//组长更新名字在上面
            }else{
                [self.dataArr addObjectsFromArray: result.groupList];
                if (self.dataArr.count) {
                    [self configDropDownView];
                }
            }
            self.model = result;//赋值给当前collectioncell的model 刷新数据
            [self.collectionView reloadData];
            NSLog(@"--- %ld个", result.companyList.count);
            if (self.visitHeaderViewDidLoadFinishCompanyArray) {
                //传递公司数组 用作筛选条件 egId(组员传eg)
                NSLog(@"%ld", result.companyList.count);
                self.visitHeaderViewDidLoadFinishCompanyArray(result.companyList, result.egId, result.epId);
            }
        }else{
            [QSAppKeyWindow hideHud];
        }
    }];
}

- (void)updateUi{
    NSString *groupNameText = [self.roleName stringByAppendingString:@"组长"];
    NSInteger lenth = groupNameText.length;
    _nameLabel.text = [NSString stringWithFormat:@"尊敬的 %@    您好！", groupNameText];
    [_nameLabel setAttributedText:[UILabel setLabelTextColorWithString:_nameLabel.text Range:NSMakeRange(4, lenth) andColor:[UIColor colorWithHexString:@"#128BEC"]]];
    [self bringSubviewToFront:self.nameLabel];
}

@end
